package org.flower.web.servlet;

import com.jspsmart.upload.*;
import org.flower.entity.User;
import org.flower.service.UserService;
import org.flower.service.impl.UserServiceImpl;
import org.omg.CORBA.StringHolder;

import javax.jws.soap.SOAPBinding;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet(value = "/PersonalCenterServlet")
public class PersonalCenterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取作用域中的用户id
        User user = (User)request.getSession().getAttribute("user");
        UserService userService = new UserServiceImpl();
        User UserList=userService.getByid(user.getId());
        Date borndate = UserList.getBorndate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateb = "";
        if(borndate!=null){
            dateb = sdf.format(borndate);
        }
        UserList.setDateBorda(dateb);
        String[] split = UserList.getDateBorda().split("-");
        List<String> list = new ArrayList<>();
        for (int i=0;i<split.length;i++){
            list.add(split[i]);
        }
        String zhi= UserList.getPhone();
        int length =zhi.length();
        //获取手机号把中间的四位变成*号
        zhi=zhi.substring(0,3)+"****"+zhi.substring(length-4);
        if (UserList!=null){
            request.getSession().setAttribute("PerSex",UserList.getSex());
            request.getSession().setAttribute("DateTime",list);
            request.getSession().setAttribute("UserList",UserList);
            request.setAttribute("phone",zhi);
            request.getRequestDispatcher("PersonalCenter.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //实例化SmartUpload对象
        SmartUpload smart = new SmartUpload();
        //设置编码格式
        smart.setCharset("UTF-8");
        //初始化
        smart.initialize(this,request,response);
        //文件上传
        try {
            smart.upload();
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
        //获取专用对象
        Request req =smart.getRequest();
        String id=req.getParameter("id");
        String realName=req.getParameter("realname");
        String sex=req.getParameter("sex");
        String year=req.getParameter("year");
        String month=req.getParameter("month");
        String day=req.getParameter("day");
        String Date = year+"-"+month+"-"+day;
        String photo_img=req.getParameter("defalut-img");
        //接收一个图片或文件
        Files files =smart.getFiles();
        File file =files.getFile(0);
        String photo="";
        //判断是否上传文件，没有上传true,上传false;
        if(file.isMissing()){
            //获取默认图片
            photo=photo_img;
        }else {
            //将图片保存到tomcat服务器中，并保存到数据库
            try {
                file.saveAs("/image/user/"+file.getFileName());
                photo="image/user/"+file.getFileName();
            } catch (SmartUploadException e) {
                e.printStackTrace();
            }
        }
        //将参数和文件路径保存到对象
        PrintWriter writer= response.getWriter();
        User user1 = new User();
        user1.setId(Integer.parseInt(id));
        user1.setName(realName);
        user1.setSex(sex);
        user1.setPhoto(photo);
        user1.setDateBorda(Date);
        UserService userService = new UserServiceImpl();
        String[] split = user1.getDateBorda().split("-");
        List<String> list = new ArrayList<>();
        for (int i=0;i<split.length;i++){
          list.add(split[i]);
        }
        request.getSession().setAttribute("PerSex",user1.getSex());
        request.getSession().setAttribute("DateTime",list);
        if(userService.UpdateById(user1)){
            writer.write(
                    "<script type=\"text/javascript\">alert('保存成功！');setInterval(\"\",10);parent.location.href='" +
                            request.getContextPath()
                            + "/PersonalCenterServlet'</script>");
            writer.flush();
            writer.close();
        }else {
            writer.write(
                    "<script type=\"text/javascript\">alert('保存成功！');setInterval(\"\",10);parent.location.href='" +
                            request.getContextPath()
                            + "/PersonalCenter.jsp'</script>");
            writer.flush();
            writer.close();
        }
    }
}
